package org.example.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JwtUtil {

	//接收业务数据,生成token并返回
    public String genToken(Map<String, Object> claims, String userSecretKey, long userTtl) {
        return JWT.create()
                .withClaim("claims", claims) // 添加载荷，可以添加多个，用Map封装
                .withExpiresAt(new Date(System.currentTimeMillis() + userTtl)) // 设置过期时间
                .sign(Algorithm.HMAC256(userSecretKey)); // 设置加密算法,key是密钥
    }

	//接收token,验证token,并返回业务数据
    public Map<String, Object> parseToken(String token, String userSecretKey) {
        return JWT.require(Algorithm.HMAC256(userSecretKey))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
